package com.koi.classservice.Utils;

import com.koi.classservice.domain.po.CourseTest;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

// 课程数据反向解析工具类
public class CourseDataReverseParser {
    private static final String[] DAYS = {"一", "二", "三", "四", "五"};
    private static final String[] SESSIONS = {"1-2", "3-4", "5-6", "7-8", "9-10", "11-12"};
    private static final int MAX_WEEK = 20; // 假设一个学期最大周次为 20


    public static CourseTest parseCourses(String data) {

        CourseTest courseTest=new CourseTest();
        String []list1=data.split(",");
        String ke=list1[list1.length-1];
        List<Integer> list2=new ArrayList<>();
        for (int i = 0; i < list1.length-1; i++) {
            List<Integer>f=parseWeeks(list1[i],ke);
            for (int j = 0; j < f.size(); j++) {
                list2.add(f.get(j));
            }
        }
        courseTest.setKeTime(ke);
        courseTest.setWeek(list2);



        return courseTest;
    }

//    public static List<ReverseCourseSchedule> parseCourses(List<Course> courses) {
//        List<ReverseCourseSchedule> scheduleList = new ArrayList<>();
//        for (Course course : courses) {
//            String weekStr = course.getWeek();
//            String sessionStr = course.getSession();
//            String classroom = course.getClassroom();
//            String campus = course.getCampus();
//
//            String day = parseDay(sessionStr);
//            if (day == null) {
//                continue;
//            }
//
//            String session = parseSessionRange(sessionStr);
//            Boolean isSingle = parseIsSingle(sessionStr);
//            List<Integer> weeks = parseWeeks(weekStr, sessionStr);
//
//
//
//
//        }
//        return scheduleList;
//    }

    //解析周次+单双周
    private static List<Integer> parseWeeks(String weekStr, String sessionStr) {
        List<Integer> weeks = new ArrayList<>();
        String[] ranges = weekStr.split(",");
        boolean isSingle = sessionStr.contains("单");
        boolean isDouble = sessionStr.contains("双");

        for (String range : ranges) {
            if (range.contains("-")) {
                String[] parts = range.split("-");
                int start = Integer.parseInt(parts[0]);
                int end = Integer.parseInt(parts[1]);
                if (isSingle) {
                    for (int i = start; i <= end; i += 2) {
                        weeks.add(i);
                    }
                } else if (isDouble) {
                    for (int i = start + (start % 2 == 0 ? 0 : 1); i <= end; i += 2) {
                        weeks.add(i);
                    }
                } else {
                    for (int i = start; i <= end; i++) {
                        weeks.add(i);
                    }
                }
            } else {
                int week = Integer.parseInt(range);
                if ((!isSingle && !isDouble) ||
                        (isSingle && week % 2 == 1) ||
                        (isDouble && week % 2 == 0)) {
                    weeks.add(week);
                }
            }
        }
        return weeks;
    }

    //解析周几
    private static String parseDay(String sessionStr) {
        Pattern pattern = Pattern.compile("([一二三四五])");
        Matcher matcher = pattern.matcher(sessionStr);
        if (matcher.find()) {
            return matcher.group(1);
        }
        return null;
    }

    //解析第几节
    private static String parseSessionRange(String sessionStr) {
        Pattern pattern = Pattern.compile("\\[([0-9\\-]+)节\\]");
        Matcher matcher = pattern.matcher(sessionStr);
        if (matcher.find()) {
            return matcher.group(1);
        }
        return null;
    }

    //解析单双周
    private static Boolean parseIsSingle(String sessionStr) {
        if (sessionStr.contains("单")) {
            return true;
        } else if (sessionStr.contains("双")) {
            return false;
        }
        return null;
    }

    // 反向处理星期
    private static String reverseDay(String day) {
        int index = java.util.Arrays.asList(DAYS).indexOf(day);
        if (index != -1) {
            return DAYS[DAYS.length - 1 - index];
        }
        return null;
    }

    // 反向处理节次
    private static String reverseSession(String session) {
        int index = java.util.Arrays.asList(SESSIONS).indexOf(session);
        if (index != -1) {
            return SESSIONS[SESSIONS.length - 1 - index];
        }
        return null;
    }
}